package com.mine.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mine.common.utils.Page;
import com.mine.dao.EvaluateMapper;
import com.mine.dao.MineMallEvaluateMapper;
import com.mine.pojo.MallEvaluateData;
import com.mine.pojo.MineMallEvaluate;
import com.mine.pojo.MineMallEvaluateExample;
import com.mine.pojo.MineMallEvaluateExample.Criteria;
import com.mine.pojo.MineUser;
import com.mine.result.CodeMsg;
import com.mine.result.Result;
import com.mine.vo.EvaluateVo;

@Service
public class EvaluateServiceImpl implements EvaluateService{

	@Autowired
	MineMallEvaluateMapper mineMallEvaluateMapper;

	@Autowired
	UserService userService;
	
	@Autowired
	EvaluateMapper eMapper;
	
	//评价商品
	@Override
	public Result saveEvaluateToGoods(String token, MineMallEvaluate mallEvaluate) {
	
		return saveEvaluate(token, mallEvaluate);
	}

	//评价店铺
	@Override
	public Result saveEvaluateToShop(String token, MineMallEvaluate mallEvaluate) {
		
		return saveEvaluate(token, mallEvaluate);
	}

	//保存评价内容
	private Result saveEvaluate(String token, MineMallEvaluate mallEvaluate) {
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		mallEvaluate.setUserId(user.getId());
		mallEvaluate.setEvaluateTime(new Date());
		//mallEvaluate.setStatus((byte)0);
		mineMallEvaluateMapper.insertSelective(mallEvaluate);
		
		return Result.ok();
	}

	@Override
	public Result getGoodsEvaluate(Long goodsId, Integer size, Integer page) {
		
		//拼装查询条件
		EvaluateVo evaluateVo = new EvaluateVo();
		evaluateVo.setType((byte)0);//type=0表示用户对商品的评价 1表示用户对商铺的评价 2表示商铺对用户的评价
		evaluateVo.setStatus((byte)1);
		evaluateVo.setGoodsId(goodsId);
		evaluateVo.setSize(size);
		evaluateVo.setPage(page);
		evaluateVo.setStart((page-1)*size);
		//System.out.println(evaluateVo);
		List<MallEvaluateData> findEvaluateList = eMapper.findEvaluateList(evaluateVo );
		//System.out.println(findEvaluateList);
		Integer count = eMapper.findEvaluateListCount(evaluateVo);
		//System.out.println(count);
		Page<MallEvaluateData> mallPage= new Page<MallEvaluateData>();
		//System.out.println(mallPage);
		mallPage.setRows(findEvaluateList);
		mallPage.setPage(page);
		mallPage.setSize(size);
		mallPage.setTotal(count);
		
		return Result.success(mallPage);
	}

	@Override
	public Result updateEvaluateHid(String token, Long id) {
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		MineMallEvaluate record = new MineMallEvaluate();
		record.setId(id);
		record.setStatus((byte)0);
		
		return updateEvaluate(token, record);
	}

	@Override
	public Result updateEvaluateToGoods(String token, MineMallEvaluate mallEvaluate) {
		
		return updateEvaluate(token, mallEvaluate);
	}

	@Override
	public Result updateEvaluateToShop(String token, MineMallEvaluate mallEvaluate) {
		
		return updateEvaluate(token, mallEvaluate);
	}
	
	private Result updateEvaluate(String token, MineMallEvaluate mallEvaluate) {
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		mineMallEvaluateMapper.updateByPrimaryKeySelective(mallEvaluate);
		return Result.ok();
	}

	@Override
	public Result deleteEvaluate(String token, Long id) {
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		MineMallEvaluateExample example = new MineMallEvaluateExample();
		Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(user.getId());
		criteria.andIdEqualTo(id);
		mineMallEvaluateMapper.deleteByExample(example );
		return Result.ok();
	}
}
